package com.zjq.smartdevice.service.impl;

import com.zjq.smartdevice.dto.HumitureEchartsDto;
import com.zjq.smartdevice.mapper.SysHumitureMapper;
import com.zjq.smartdevice.pojo.SysHumiture;
import com.zjq.smartdevice.service.HumitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @User zhangjiaqi
 * @Project project
 * @Package com.zjq.smartdevice.service.impl
 * @Date 2022/3/27 21:35
 */
@Service
@Transactional
public class HumitureServiceImpl implements HumitureService {

    private SysHumitureMapper sysHumitureMapper;

    @Autowired
    public void setSysHumitureMapper(SysHumitureMapper sysHumitureMapper) {
        this.sysHumitureMapper = sysHumitureMapper;
    }

    @Override
    public HumitureEchartsDto findOneHourData() {
        //如何找到一个小时之前的时间
        Instant now = Instant.now();//当前时间
        Instant instant = now.plusSeconds(-3600);//以当前时间为准向前推3600秒
        List<SysHumiture> humitureList = sysHumitureMapper.findDataByTime(Date.from(instant), Date.from(now));
        List<java.util.Date> times = humitureList.stream().map(SysHumiture::getUploadTime).collect(Collectors.toList());
        List<Double> temperatureList = humitureList.stream().map(SysHumiture::getTemperature).collect(Collectors.toList());
        List<Double> humidityList = humitureList.stream().map(SysHumiture::getHumidity).collect(Collectors.toList());
        HumitureEchartsDto humitureEchartsDto = new HumitureEchartsDto();
        humitureEchartsDto.setTimes(times);
        humitureEchartsDto.setTemperatureList(temperatureList);
        humitureEchartsDto.setHumidityList(humidityList);
        return humitureEchartsDto;
    }
}
